mini.parse();
var formShow;
var clickStatus;        //列表跳转页面时是双击还是点击编辑按钮
formInit();

// formShow=JSON.parse(formShow);
console.log(formShow);
cTableName=formShow.cTableName;
code=formShow.code;
tableChClassName = formShow.tableChClassName
var payMentOrderId; // 表单id

var form;
var grid;

var add=mini.get("add"); // 增加按钮
var edit=mini.get("edit"); // 编辑按钮
var save=mini.get("save"); // 保存按钮
var remove=mini.get("remove");  // 取消按钮
var addd =mini.get("addd"); // 新增行按钮
var removeRow =mini.get("removeRow"); // 删除行按钮
var copy =mini.get("copy"); // 复制行按钮
var abandonTrial = mini.get("abandonTrial"); // 弃审按钮
var review = mini.get("review"); // 审核按钮

form.setData(formShow.payMentOrderPage[0]);
grid.setData(formShow.payMentOrderPageTableBody);


// 初始化
// 初始化表单
function formInit() {
    //单据列表页跳转时数据
    formShow = localStorage.getItem("formShow");
   // 按钮跳转，add/edit
    formShow = $.parseJSON(formShow);
    console.log(formShow, 'formShow');             //按钮状态（编辑/新增）
    clickStatus = formShow.clickStatus;       //双击跳转页面还是点击编辑跳转页面
    payMentOrderId = formShow.payMentOrderId;

    form = new mini.Form("formName");
    grid = mini.get("datagrid1");

    form.setEnabled(false);
}

// 根据状态初始化按钮状态
if(clickStatus == 'rowdblclick'){
    add.hide();
    edit.hide();
    save.hide();
    remove.hide();
    abandonTrial.hide();
    review.hide();

    addd.disable();
    removeRow.disable();
    copy.disable();

    form.setEnabled(false);
    grid.setEnabled(false);
}
else if(clickStatus == 'edit'){
    if (formShow.payMentOrderPage[0].payMentState == "未审核") {
        add.disable();
        edit.enable();
        save.disable();
        remove.enable();
        review.enable();
        abandonTrial.disable();

        addd.disable();
        removeRow.disable();
        copy.disable();

        form.setEnabled(false);
        grid.setEnabled(false);
    } else { // 已审核
        add.disable();
        edit.disable();
        save.disable();
        remove.disable();
        review.disable();

        addd.disable();
        removeRow.disable();
        copy.disable();

        form.setEnabled(false);
        grid.setEnabled(false);
    }
}
else if(clickStatus == 'add'){ //  付款->  保存可用
    add.disable();
    edit.disable();
    // save.disable();
    remove.disable();
    review.disable();
    abandonTrial.disable();
    form.setEnabled(false);

}

// 提交单元格编辑数据前激发
function cellendedit(e) {
    var editor = e.editor;
    console.log(e.row)
    var rowElement = e.row;
    var amountPaid = rowElement.amountPaid;//实付金额
    var amountPayable = rowElement.amountPayable;//应付金额
    if (parseInt(amountPaid) > parseInt(amountPayable)) {
        mini.alert("输入金额大于应付金额");
        return;
    } else {
        rowElement.surplusamount = amountPayable - amountPaid;
        grid.updateRow(e.row,e.row);
    }
}

function add() {
    var form=new mini.Form("formName");
    var data=form.getData();
    return data;
    console.log(data);
}

// 编辑修改
function edit() {

    edit.disable();
    review.disable();
    save.enable();
    remove.enable();

    addd.enable();
    removeRow.enable();
    copy.enable();

    grid.setEnabled(true);


}


// 保存
function save() {

    // 校验单元格内容填写规则
    grid.validate();
    if (grid.isValid() == false) {
        alert("请校验输入单元格内容")
        var error = grid.getCellErrors()[0];
        grid.beginEditCell(error.record,error.column);
        return;
    }

    var data1=form.getData();
    console.log(data1);
    data1.documentDate=formatDate(data1.documentDate);
    console.log(data1);
    data3=JSON.stringify(data1);
    console.log(data3);
    //子表数据
    var data2=grid.getData();
    console.log(data2);
    //转换实付金额的数据类型（保存时转化为字符串）
    data2.forEach(function (item) {
        item.amountPayable = item.amountPayable + '';
        item.amountPaid = item.amountPaid + '';
        item.surplusamount = item.surplusamount + '';
    })
    data4=JSON.stringify(data2);
    console.log(data4);
    $.ajax({
        url: ajaxUrl + "/PaymentOrder/savePaymentOrder.do",
        type:"post",
        data:{
            payMentOrderId:payMentOrderId,
            form:data3,
            datagrid:data4
        },
        success:function (data) {
            console.log(data);
            payMentOrderId = data.formId;
            console.log(payMentOrderId);
            mini.alert(data.success);

            edit.enable();

            add.disable();
            save.disable();
            remove.enable();
            abandonTrial.disable();

            addd.disable();
            remove.disable();
            copy.disable();

            form.setEnabled(false);
            grid.setEnabled(false);

            review.enable();
        },
        error:function (data) {

        }
    })


}

// 审核
function review() {
    console.log(cTableName);
    console.log(code);
    console.log(payMentOrderId);
    $.ajax({
        url: ajaxUrl + "/PaymentOrder/checkPaymentOrder.do",
        type:"post",
        data:{
            cTableName:cTableName,
            code:code,
            payMentOrderId:payMentOrderId,
            tableChClassName:tableChClassName
        },
        success:function (data) {
            console.log(data);
            if (data == "审核通过") {
                review.disable();
                edit.disable();
                abandonTrial.enable();
                remove.disable();
                mini.alert("审核成功")
            } else if (data == "付款金额大于收款金额，核对信息") {
                mini.alert("付款金额大于收款金额，核对信息")
            } else if (data == "不能重复审核") {
                mini.alert('不能重复审核');
            } else {
                mini.alert("审核失败")
            }
        },
        error:function (data) {
            console.log("没成功");
        }
    })
}

// 弃审
function abandonTrial() {
    $.ajax({
        url:ajaxUrl + "/PaymentOrder/abandonTrialPaymentOrder.do",
        type:"post",
        data:{
            cTableName:cTableName,
            code:code,
            payMentOrderId:payMentOrderId,
            tableChClassName:tableChClassName
        },
        success:function (data) {
            if (data == "弃审成功") {
                add.disable();
                edit.enable();
                save.disable();
                remove.enable();

                addd.disable();
                removeRow.disable();
                abandonTrial.disable();

                form.setEnabled(false);
                grid.setEnabled(false);
            }
            else if (data == "不能重复弃审") {
                mini.alert('不能重复弃审');
            }
        },
        error:function (data) {
            alert("失败")
        }
    })
}

// 复制行
function copyRow() {
    var row=grid.getSelected();
    var newRow=mini.clone(row);
    grid.addRow(newRow);
}

//转时间格式(yyyy-MM-dd)
function formatDate(datetime) {
    var date = new Date(datetime);
    var year = date.getFullYear(),
        month = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1):date.getMonth() + 1,
        day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return year + '-' + month + '-' + day;
}

// 删除
function remove() {
    mini.confirm("确定删除？","确定？",
        function (action) {
            if (action == "ok") {
                $.ajax({
                    url:ajaxUrl + "/PaymentOrder/removePayMentOrder.do",
                    type:"post",
                    data:{
                        payMentOrderId:payMentOrderId
                    },
                    success:function (data) {
                        if (data.data == "success") {
                            mini.alert("删除成功");
                        }
                    },
                    error:function () {
                        mini.alert("删除失败");
                    }
                })
            } else {

            }
        })
}

//新增行
function addRow() {
    var newRow={name:""};
    grid.addRow(newRow,0);

    grid.beginEditCell(newRow,"LoginName");

}

//删除行
function removeRows() {
    var rows=grid.getSelecteds();
    if(rows.length>0){
        grid.removeRows(rows ,true);
    }
}
// 时间戳转为日期格式
// function timestampToTime(timestamp) {
//     var date = new Date();// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
//     Y = date.getFullYear() + '-';
//     M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
//             .getMonth() + 1)
//         + '-';
//     D = date.getDate() + ' ';
//     h = date.getHours() + ':';
//     m = date.getMinutes() + ':';
//     s = date.getSeconds();
//     return Y + M + D ;
// }

//截取字符串_方法
//var data="aaa-bbb";
//intercept(data,0);输出为aaa
//intercept(data,1)输出
// function intercept() {
//     var index=obj.lastIndexOf("\_");
//     if(state==0){
//         obj=obj.substring(0,index);
//     }else{
//         obj=obj.substring(index+1,obj.length);
//     }
// }